package de.oszimt.matse.messenger;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.oszimt.matse.messenger.exceptions.LoginFailedException;
import de.oszimt.matse.messenger.exceptions.UserAlreadyInContactListException;
import de.oszimt.matse.messenger.exceptions.UserDoesNotExistException;

@Controller
public class MessengerController 
{
	@Autowired
	@Qualifier("user")
	User user;
	
	@Autowired
	@Qualifier("service")
	MessengerService service;
	
    @RequestMapping("/")
    public ModelAndView start(HttpSession session) throws UserDoesNotExistException
    {
    	if (this.user.getName() == null)
    	{
    		SecurityContext securityContext = (SecurityContext)session.getAttribute("SPRING_SECURITY_CONTEXT");
    		this.service.initializeUser(this.user, securityContext.getAuthentication().getName());
    	}
    	this.service.setInitialMessages(this.user);
    	this.service.dispatchActivationFlags(this.user, true);
    	ModelAndView view = new ModelAndView("main");
        view.addObject("ownUserName", this.user.getName());
        view.addObject("styleName", this.user.getStyle());
        return view;
    }

    @RequestMapping("/updateStyle")
    public void updateStyle(@RequestParam("styleName") String styleName)
    {
    	this.user.setStyle(styleName);
    }

    @RequestMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws LoginFailedException, ServletException, IOException, UserDoesNotExistException
    {
		this.service.logout(this.user);
    }
    
    @RequestMapping("/getMessages")
    public ModelAndView getMessages() 
    {
		ModelAndView view = new ModelAndView("Simple");
		view.addObject("msg", this.service.getJSONMessages(user));
		return view;
    }
    
    @RequestMapping("/sendTextMessage")
    public void textMessageReceiver(@RequestParam("receiver") String receiverName, @RequestParam("message") String message) throws UserDoesNotExistException
    {
    	this.service.processTextMessage(this.user, receiverName, message);
    }
    
    @RequestMapping("/extendContact")
    public void extendContact(@RequestParam("newUser") String newUser) throws UserDoesNotExistException, UserAlreadyInContactListException
    {
    	this.service.extendContact(this.user, newUser);
    }
    
    @RequestMapping("/reduceContact")
    public void reduceContact(@RequestParam("userToRemove") String userToRemove) throws UserDoesNotExistException
    {
    	this.service.reduceContact(this.user, userToRemove);
    }
    
}
